package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn00160Bo;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Sequence;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
/**
 * 业务废止
 */
public class TransferSeniorSearchStopController extends TmsController {
	private Logger logger = Logger.getLogger(TransferSeniorSearchStopController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		Sequence seq = Sequence.getInstance();
		try {
			controller.begin();
			String serial = (String)uc.getAttribute("serial");
			TmsUserProfile profile = uc.getUserProfile();
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			String transferSerial = seq.getSerial(6);
			accept = accept.getAcceptInfoBySerial(serial, "");
			if(accept!=null)
			{
				accept.setStatus(16);
				accept.setUpdateUserId(profile.getUniqueId());
				accept.updateAcceptStatusBySerial(accept);
				//如果车辆是库存车辆
				if(accept.getMarketType() == 1)
				{
					Car car = Car.getInstance();
					//modify by huangheli at 20151016 start
					car = car.findCarBySerial(accept.getBasicCarId());
					car.setStatus(1);
					//car.setStatus(4);
					//car.setRegisterStatus(4);
					//modify by huangheli at 20151016 end
					car.setUpdateUserId(profile.getUniqueId());
					car.setSerial(accept.getBasicCarId());
					//modify by huangheli at 20151016 start
					//car.updateCarStatus(car);
					car.updateCarStatusOnly(car);
					//modify by huangheli at 20151016 end
				}
				//如果车辆是非库存车辆
				if(accept.getMarketType() == 2)
				{
					TransferPersonalCarInfo person = new TransferPersonalCarInfo();
					person = person.getPersonalCarInfoBySerial(accept.getBasicCarId());
					person.setStatus(4);
					person.setSerial(accept.getBasicCarId());
					person.updateCarStatus(person);
				}
				
				//workload:3.1.2
				
//				//add by huangheli at 20151016 start
//				//插入 过户业务基本信息表（tms_transfer_accepted_info）
//				
//				TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance();
//				transferAcceptedInfo.setSerial(transferSerial);
//				transferAcceptedInfo.setMarketType(1);
//				transferAcceptedInfo.setCarClass(2);
//				transferAcceptedInfo.setStatus(0);
//				transferAcceptedInfo.setSourceType(1);
//				transferAcceptedInfo.setLicensePlate(accept.getLicensePlate());
//				transferAcceptedInfo.setBasicCarId(accept.getBasicCarId());
//				transferAcceptedInfo.setMemberId(accept.getMemberId());
//				transferAcceptedInfo.setInsertUserid(profile.getUniqueId());
//				transferAcceptedInfo.setUpdateUserId(profile.getUniqueId());
//				
//				transferAcceptedInfo.insertCarResumeInfo(transferAcceptedInfo);
				//add by huangheli at 20151016 end
				//历史记录
				TransferProcessHistory history = new TransferProcessHistory();
				history.setSerial(serial);
				history.setBusinessType(39);
				history.setInsertUserId(profile.getUniqueId());
				history.setReason("");
				history.insertHisInfoAtAssess(history);
				
				
				//20160313
//				history = new TransferProcessHistory();
//				history.setSerial(transferSerial);
//				history.setBusinessType(0);//入库待查验
//				history.setInsertUserId(profile.getUniqueId());
//				history.setReason("");
//				history.insertHisInfoAtAssess(history);
				//
				//插入”tms_transfer_finance_info（过户业务的财务收费信息表）“
//				TransferFinanceInfo transferFinanceInfo = new TransferFinanceInfo();
//				transferFinanceInfo.insertFinanceByInner(transferSerial);
				
				//
				uc.setAttribute("tishixinxi", "1");
			}
			// add by huangheli 2016/01/09 start
			MobileMessage message = new MobileMessage();
			message.putValue("Serial", serial);
			message.setMessage("过户同步[高级处理 - 业务作废]接口,过户流水号为#Serial#过户信息不存在，无法同步TMS的高级处理（业务作废）");
			TransferDataSyn00160Bo bo = new TransferDataSyn00160Bo();
			bo.setMobileMessage(message);
			bo.setSerial(serial);
			bo.setNewSerial(transferSerial);
			bo.setFunc(13);
//			workload:3.1.2 delete
//			//如果车辆是库存车辆
//			if(accept.getMarketType() == 1){
//				bo.setStatus(1);
//				bo.setDetailStatus(0);
//			}
//			//如果车辆是非库存车辆
//			if(accept.getMarketType() == 2){
				bo.setStatus(16);
				bo.setDetailStatus(39);
//			}
			PullingExecutor.getInstance().submit(bo);	
			// add by huangheli 2016/01/09 end
			
			
			//
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");

	}

}
